package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.brokerageconfig.BrokerageConfigBo;
import org.summer.lawyer.domain.vo.brokerageconfig.BrokerageConfigVo;
import org.summer.lawyer.service.IBrokerageConfigService;

import java.util.List;

/**
 * 分销配置
 *
 * @author summer
 * @date 2024-06-20
 */
@Tag(name = "后台相关接口", description = "分销配置")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/brokerageConfig")
public class BrokerageConfigController extends BaseController {

    private final IBrokerageConfigService brokerageConfigService;

    /**
     * 查询分销配置列表
     */
    @Operation(summary = "查询分销配置列表", description = "查询分销配置列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:config:list")
    @GetMapping("/list")
    public TableDataInfo<BrokerageConfigVo> list(BrokerageConfigBo bo, PageQuery pageQuery) {
        return brokerageConfigService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出分销配置列表
     */
    @Operation(summary = "导出分销配置列表", description = "导出分销配置列表")
    @SaCheckPermission("brokerage:config:export")
    @Log(title = "分销配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BrokerageConfigBo bo, HttpServletResponse response) {
        List<BrokerageConfigVo> list = brokerageConfigService.selectBrokerageConfigVoList(bo);
        ExcelUtil.exportExcel(list, "分销配置", BrokerageConfigVo.class, response);
    }

    /**
     * 获取分销配置详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "查询分销配置列表", description = "查询分销配置列表", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:config:query")
    @SaCheckLogin
    @GetMapping("/config")
    public R<BrokerageConfigVo> getInfo() {
        return R.ok(brokerageConfigService.selectOneBrokerageConfigVo());
    }


    /**
     * 新增分销配置
     */
    @Operation(summary = "新增分销配置", description = "新增分销配置", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:config:add")
    @Log(title = "分销配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveBrokerageConfig")
    public R<Void> saveBrokerageConfig(@Validated(AddGroup.class) @RequestBody BrokerageConfigBo bo) {
        return toAjax(brokerageConfigService.insertByBo(bo));
    }

    /**
     * 修改分销配置
     */
    @Operation(summary = "修改分销配置", description = "修改分销配置", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:config:edit")
    @Log(title = "分销配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateBrokerageConfig")
    public R<Void> updateBrokerageConfig(@Validated(EditGroup.class) @RequestBody BrokerageConfigBo bo) {
        return toAjax(brokerageConfigService.updateByBo(bo));
    }

    /**
     * 删除分销配置
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除分销配置", description = "删除分销配置", responses = { @ApiResponse( responseCode = "200", description = "成功")})
    @SaCheckPermission("brokerage:config:remove")
    @Log(title = "分销配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBrokerageConfig/{ids}")
    public R<Void> removeBrokerageConfig(@NotEmpty(message = "主键不能为空")
                                         @PathVariable Long[] ids) {
        return toAjax(brokerageConfigService.deleteWithValidByIds(List.of(ids), true));
    }
}
